﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;

//<div page="1" part="1">
//<h1 class="title"></h1>
//<p class="part_des">title</p>
//<table>

//<tr><td></td><td>1</td><td>2</td></tr>
//<tr><td>question1</td><td>radio</td><td>radio</td></tr>

//</table>

//<table class="question">

//</table>
//</div>

//<div id="page2"> ...

public class PageBuilder
{
    int pageSize;
    public PageBuilder(int pageSize)
    {
        this.pageSize = pageSize;
    }

    public HtmlGenericControl GetPages(IEnumerable<Part> parts)
    {
        HtmlGenericControl div = new HtmlGenericControl("div");
        int pagenum = 1;

        foreach (Part part in parts)
        {
            for (int i = 0; i <= (part.Questions.Count - 1) / pageSize; i++)
            {
                HtmlGenericControl page = getPageDiv(pagenum, part.No);
                HtmlGenericControl title = getTitle(part.Title);
                HtmlGenericControl part_des = getPartDescription(part.Description);
                HtmlGenericControl mark_table = getMarkTable(part);
                HtmlGenericControl que_table = getQuestionTable(part, i * pageSize);

                page.Controls.Add(title);
                page.Controls.Add(part_des);
                page.Controls.Add(mark_table);
                page.Controls.Add(que_table);
                div.Controls.Add(page);
                pagenum++;
            }
        }
        return div;
    }

    HtmlGenericControl getQuestionTable(Part part, int from)
    {
        HtmlTable table = new HtmlTable();
        table.Attributes["width"] = "100%";
        int des_width = 20;
        int res_num = part.MaxMark + (part.HasYesno ? 1 : 0);
        int res_width = (100 - des_width) / res_num;
        HtmlTableRow title = new HtmlTableRow();
        HtmlTableCell des = new HtmlTableCell();
        des.Attributes["width"] = des_width + "%";
        title.Controls.Add(des);
        if (part.HasYesno)
        {
            HtmlTableCell yesno = new HtmlTableCell();
            yesno.Attributes["width"] = res_width + "%";
            yesno.InnerHtml = part.YesnoDescription;
            title.Controls.Add(yesno);
        }
        for (int i = 1; i <= part.MaxMark; i++)
        {
            HtmlTableCell mark = new HtmlTableCell();
            mark.Attributes["width"] = res_width + "%";
            mark.InnerHtml = i.ToString();
            title.Controls.Add(mark);
        }
        table.Controls.Add(title);
        int count = 0;
        for (int no = 0; no < pageSize; no++)
        {
            if (from + no >= part.Questions.Count)
                break;
            Question que = part.Questions[from + no];
            HtmlTableRow row = new HtmlTableRow();
            table.Controls.Add(row);

            {
                HtmlTableCell cell = new HtmlTableCell();
                cell.InnerHtml = que.No + " " + que.Description;
                row.Controls.Add(cell);
            }

            if (part.HasYesno)
            {
                HtmlTableCell cell = new HtmlTableCell();
                HtmlInputRadioButton rd = new HtmlInputRadioButton();
                rd.Checked = false;
                rd.Name = FormContract.GetQuestionName(que.ID);
                rd.Value = "0";
                cell.Controls.Add(rd);
                row.Controls.Add(cell);
            }

            for (int i = 1; i <= part.MaxMark; i++)
            {
                HtmlTableCell cell = new HtmlTableCell();
                HtmlInputRadioButton rd = new HtmlInputRadioButton();
                rd.Name = FormContract.GetQuestionName(que.ID);
                rd.Value = i.ToString();
                cell.Controls.Add(rd);
                row.Controls.Add(cell);
            }
            count++;
        }

        count %= pageSize;
        if (count != 0)
        {
            for (; count < this.pageSize; count++)
            {
                HtmlTableRow row = new HtmlTableRow();
                table.Controls.Add(row);
                row.Cells.Add(new HtmlTableCell());
                if (part.HasYesno)
                    row.Cells.Add(new HtmlTableCell());
                for (int i = 1; i <= part.MaxMark; i++)
                    row.Cells.Add(new HtmlTableCell());
            }
        }
        HtmlGenericControl div = new HtmlGenericControl("div");
        div.Attributes["class"] = "que_table";
        div.Controls.Add(table);
        return div;
    }

    HtmlGenericControl getMarkTable(Part part)
    {
        HtmlTable table = new HtmlTable();
        table.Attributes["class"] = "mark_table";
        table.Attributes["width"] = (part.MaxMark*80).ToString();
        HtmlTableRow titles = new HtmlTableRow();
        HtmlTableRow contents = new HtmlTableRow();

        table.Controls.Add(titles);
        table.Controls.Add(contents);

        for (int i = 1; i <= part.MaxMark; i++)
        {
            HtmlTableCell cell = new HtmlTableCell();
            cell.Attributes["width"] = (100 / part.MaxMark) + "%";
            cell.InnerHtml = i.ToString();
            titles.Controls.Add(cell);
        }

        for (int i = 1; i <= part.MaxMark; i++)
        {
            HtmlTableCell cell = new HtmlTableCell();
            if (part.MarkDescription.ContainsKey(i))
                cell.InnerHtml = part.MarkDescription[i];
            contents.Controls.Add(cell);
        }
        HtmlGenericControl div = new HtmlGenericControl("div");
        div.Attributes["class"] = "mark_table";
        div.Controls.Add(table);
        return div;
    }

    HtmlGenericControl getPartDescription(string contents)
    {
        HtmlGenericControl div = new HtmlGenericControl("p");
        div.Attributes["class"] = "part_des";
        div.InnerHtml = contents;
        return div;
    }

    HtmlGenericControl getTitle(string contents)
    {
        HtmlGenericControl div = new HtmlGenericControl("h1");
        div.Attributes["class"] = "title";
        div.InnerHtml = contents;
        return div;
    }

    HtmlGenericControl getPageDiv(int page, int part)
    {
        HtmlGenericControl container = new HtmlGenericControl("div");
        container.Attributes["class"] = "part";
        container.Attributes["page"] = page.ToString();         
        container.Attributes["part"] = part.ToString();
        container.Attributes["style"] = "display:none";

        return container;
    }
}
